import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SoundServer {
    private ServerSocket providerSocket;
    private Socket connection = null;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    private Chat chat;

    SoundServer()
    {
        chat = new Chat("Server");
        chat.writeSystemMessage("Start server application");
    }

    public void start()
    {
        chat.writeSystemMessage("Waiting for connection...");
        do {
            try
            {
                providerSocket = new ServerSocket(8020, 10);
                connection = providerSocket.accept();
                out = new ObjectOutputStream(connection.getOutputStream());
                out.flush();
                in = new ObjectInputStream(connection.getInputStream());
            }
            catch (IOException e)
            {
                System.out.println("Error " + e);
                return;
            }
        } while (in == null || out == null);
        chat.writeSystemMessage("Get connection to port " + connection.getPort() + " from address " + connection.getInetAddress());
        chat.setIn(in);
        chat.setOut(out);
        chat.start();
    }

    public static void main(String args[])
    {
        SoundServer server = new SoundServer();
        server.start();
    }
}
